package org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.navigator;

import org.admanager.simplifiedsca.plugin.simplifiedsca.ComponentReference;
import org.admanager.simplifiedsca.plugin.simplifiedsca.ComponentService;
import org.admanager.simplifiedsca.plugin.simplifiedsca.DocumentRoot;
import org.admanager.simplifiedsca.plugin.simplifiedsca.Reference;
import org.admanager.simplifiedsca.plugin.simplifiedsca.Service;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ComponentEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ComponentNameEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ComponentReferenceEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ComponentReferenceNameEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ComponentReferenceTargetEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ComponentServiceEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ComponentServiceNameEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.CompositeEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.CompositeNameEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.DocumentRootEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ReferenceEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ReferenceNameEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ReferencePromoteEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ServiceEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ServiceNameEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.edit.parts.ServicePromoteEditPart;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.part.SimplifiedSCADiagramEditorPlugin;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.part.SimplifiedSCAVisualIDRegistry;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.providers.SimplifiedSCAElementTypes;
import org.admanager.simplifiedsca.plugin.simplifiedsca.diagram.providers.SimplifiedSCAParserProvider;
import org.eclipse.gmf.runtime.common.ui.services.parser.IParser;
import org.eclipse.gmf.runtime.common.ui.services.parser.ParserOptions;
import org.eclipse.gmf.runtime.emf.core.util.EObjectAdapter;
import org.eclipse.gmf.runtime.emf.type.core.IElementType;
import org.eclipse.gmf.runtime.notation.View;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.jface.resource.ImageRegistry;
import org.eclipse.jface.viewers.ITreePathLabelProvider;
import org.eclipse.jface.viewers.LabelProvider;
import org.eclipse.jface.viewers.TreePath;
import org.eclipse.jface.viewers.ViewerLabel;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.IMemento;
import org.eclipse.ui.navigator.ICommonContentExtensionSite;
import org.eclipse.ui.navigator.ICommonLabelProvider;

/**
 * @generated
 */
public class SimplifiedSCANavigatorLabelProvider extends LabelProvider
		implements ICommonLabelProvider, ITreePathLabelProvider {

	/**
	 * @generated
	 */
	static {
		SimplifiedSCADiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?UnknownElement", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
		SimplifiedSCADiagramEditorPlugin
				.getInstance()
				.getImageRegistry()
				.put("Navigator?ImageNotFound", ImageDescriptor.getMissingImageDescriptor()); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	public void updateLabel(ViewerLabel label, TreePath elementPath) {
		Object element = elementPath.getLastSegment();
		if (element instanceof SimplifiedSCANavigatorItem
				&& !isOwnView(((SimplifiedSCANavigatorItem) element).getView())) {
			return;
		}
		label.setText(getText(element));
		label.setImage(getImage(element));
	}

	/**
	 * @generated
	 */
	public Image getImage(Object element) {
		if (element instanceof SimplifiedSCANavigatorGroup) {
			SimplifiedSCANavigatorGroup group = (SimplifiedSCANavigatorGroup) element;
			return SimplifiedSCADiagramEditorPlugin.getInstance()
					.getBundledImage(group.getIcon());
		}

		if (element instanceof SimplifiedSCANavigatorItem) {
			SimplifiedSCANavigatorItem navigatorItem = (SimplifiedSCANavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return super.getImage(element);
			}
			return getImage(navigatorItem.getView());
		}

		return super.getImage(element);
	}

	/**
	 * @generated
	 */
	public Image getImage(View view) {
		switch (SimplifiedSCAVisualIDRegistry.getVisualID(view)) {
		case ServiceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://simplifiedsca/1.0?Service", SimplifiedSCAElementTypes.Service_3005); //$NON-NLS-1$
		case ServicePromoteEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://simplifiedsca/1.0?Service?promote", SimplifiedSCAElementTypes.ServicePromote_4002); //$NON-NLS-1$
		case CompositeEditPart.VISUAL_ID:
			return getImage(
					"Navigator?TopLevelNode?http://simplifiedsca/1.0?Composite", SimplifiedSCAElementTypes.Composite_2001); //$NON-NLS-1$
		case ReferencePromoteEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://simplifiedsca/1.0?Reference?promote", SimplifiedSCAElementTypes.ReferencePromote_4001); //$NON-NLS-1$
		case ComponentEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://simplifiedsca/1.0?Component", SimplifiedSCAElementTypes.Component_3001); //$NON-NLS-1$
		case ReferenceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://simplifiedsca/1.0?Reference", SimplifiedSCAElementTypes.Reference_3004); //$NON-NLS-1$
		case ComponentReferenceTargetEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Link?http://simplifiedsca/1.0?ComponentReference?target", SimplifiedSCAElementTypes.ComponentReferenceTarget_4003); //$NON-NLS-1$
		case ComponentServiceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://simplifiedsca/1.0?ComponentService", SimplifiedSCAElementTypes.ComponentService_3003); //$NON-NLS-1$
		case DocumentRootEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Diagram?http://simplifiedsca/1.0?DocumentRoot", SimplifiedSCAElementTypes.DocumentRoot_1000); //$NON-NLS-1$
		case ComponentReferenceEditPart.VISUAL_ID:
			return getImage(
					"Navigator?Node?http://simplifiedsca/1.0?ComponentReference", SimplifiedSCAElementTypes.ComponentReference_3002); //$NON-NLS-1$
		}
		return getImage("Navigator?UnknownElement", null); //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private Image getImage(String key, IElementType elementType) {
		ImageRegistry imageRegistry = SimplifiedSCADiagramEditorPlugin
				.getInstance().getImageRegistry();
		Image image = imageRegistry.get(key);
		if (image == null && elementType != null
				&& SimplifiedSCAElementTypes.isKnownElementType(elementType)) {
			image = SimplifiedSCAElementTypes.getImage(elementType);
			imageRegistry.put(key, image);
		}

		if (image == null) {
			image = imageRegistry.get("Navigator?ImageNotFound"); //$NON-NLS-1$
			imageRegistry.put(key, image);
		}
		return image;
	}

	/**
	 * @generated
	 */
	public String getText(Object element) {
		if (element instanceof SimplifiedSCANavigatorGroup) {
			SimplifiedSCANavigatorGroup group = (SimplifiedSCANavigatorGroup) element;
			return group.getGroupName();
		}

		if (element instanceof SimplifiedSCANavigatorItem) {
			SimplifiedSCANavigatorItem navigatorItem = (SimplifiedSCANavigatorItem) element;
			if (!isOwnView(navigatorItem.getView())) {
				return null;
			}
			return getText(navigatorItem.getView());
		}

		return super.getText(element);
	}

	/**
	 * @generated
	 */
	public String getText(View view) {
		if (view.getElement() != null && view.getElement().eIsProxy()) {
			return getUnresolvedDomainElementProxyText(view);
		}
		switch (SimplifiedSCAVisualIDRegistry.getVisualID(view)) {
		case ServiceEditPart.VISUAL_ID:
			return getService_3005Text(view);
		case ServicePromoteEditPart.VISUAL_ID:
			return getServicePromote_4002Text(view);
		case CompositeEditPart.VISUAL_ID:
			return getComposite_2001Text(view);
		case ReferencePromoteEditPart.VISUAL_ID:
			return getReferencePromote_4001Text(view);
		case ComponentEditPart.VISUAL_ID:
			return getComponent_3001Text(view);
		case ReferenceEditPart.VISUAL_ID:
			return getReference_3004Text(view);
		case ComponentReferenceTargetEditPart.VISUAL_ID:
			return getComponentReferenceTarget_4003Text(view);
		case ComponentServiceEditPart.VISUAL_ID:
			return getComponentService_3003Text(view);
		case DocumentRootEditPart.VISUAL_ID:
			return getDocumentRoot_1000Text(view);
		case ComponentReferenceEditPart.VISUAL_ID:
			return getComponentReference_3002Text(view);
		}
		return getUnknownElementText(view);
	}

	/**
	 * @generated
	 */
	private String getComponent_3001Text(View view) {
		IParser parser = SimplifiedSCAParserProvider.getParser(
				SimplifiedSCAElementTypes.Component_3001,
				view.getElement() != null ? view.getElement() : view,
				SimplifiedSCAVisualIDRegistry
						.getType(ComponentNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SimplifiedSCADiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getComposite_2001Text(View view) {
		IParser parser = SimplifiedSCAParserProvider.getParser(
				SimplifiedSCAElementTypes.Composite_2001,
				view.getElement() != null ? view.getElement() : view,
				SimplifiedSCAVisualIDRegistry
						.getType(CompositeNameEditPart.VISUAL_ID));
		if (parser != null) {
			return parser.getPrintString(new EObjectAdapter(
					view.getElement() != null ? view.getElement() : view),
					ParserOptions.NONE.intValue());
		} else {
			SimplifiedSCADiagramEditorPlugin.getInstance().logError(
					"Parser was not found for label " + 5006); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getReferencePromote_4001Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getComponentReference_3002Text(View view) {
		ComponentReference domainModelElement = (ComponentReference) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SimplifiedSCADiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3002); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getServicePromote_4002Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getDocumentRoot_1000Text(View view) {
		DocumentRoot domainModelElement = (DocumentRoot) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SimplifiedSCADiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 1000); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getService_3005Text(View view) {
		Service domainModelElement = (Service) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SimplifiedSCADiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3005); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getComponentService_3003Text(View view) {
		ComponentService domainModelElement = (ComponentService) view
				.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SimplifiedSCADiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3003); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getComponentReferenceTarget_4003Text(View view) {
		return ""; //$NON-NLS-1$
	}

	/**
	 * @generated
	 */
	private String getReference_3004Text(View view) {
		Reference domainModelElement = (Reference) view.getElement();
		if (domainModelElement != null) {
			return domainModelElement.getName();
		} else {
			SimplifiedSCADiagramEditorPlugin.getInstance().logError(
					"No domain element for view with visualID = " + 3004); //$NON-NLS-1$
			return ""; //$NON-NLS-1$
		}
	}

	/**
	 * @generated
	 */
	private String getUnknownElementText(View view) {
		return "<UnknownElement Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	private String getUnresolvedDomainElementProxyText(View view) {
		return "<Unresolved domain element Visual_ID = " + view.getType() + ">"; //$NON-NLS-1$  //$NON-NLS-2$
	}

	/**
	 * @generated
	 */
	public void init(ICommonContentExtensionSite aConfig) {
	}

	/**
	 * @generated
	 */
	public void restoreState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public void saveState(IMemento aMemento) {
	}

	/**
	 * @generated
	 */
	public String getDescription(Object anElement) {
		return null;
	}

	/**
	 * @generated
	 */
	private boolean isOwnView(View view) {
		return DocumentRootEditPart.MODEL_ID
				.equals(SimplifiedSCAVisualIDRegistry.getModelID(view));
	}

}
